<?php
if ($_SESSION['niveauAuth']=='1' or $_SESSION['niveauAuth']=='2'or $_SESSION['niveauAuth']=='3' or $_SESSION['niveauAuth']=='4'or $_SESSION['niveauAuth']=='5'or $_SESSION['niveauAuth']=='6'or $_SESSION['niveauAuth']=='7' or $_SESSION['niveauAuth']=='8') {
/**
 * Controleur permettant de lister les factures 
 */
require_once('dao/ClientDao.php');
date_default_timezone_set('Europe/Paris'); 
//liste les factures 

$requete = $bdd->prepare("
(SELECT id, type, nom, date, facture, total_facturation
FROM admin_facture
ORDER BY nom)
 UNION 
(SELECT 
    fp.id as id,
    'ponctuelle' AS type,
    cl.raison_social AS nom,
    fp.create_date AS date,
    fp.pdf AS facture,
    SUM(lfp.prix) as total_facturation
FROM
    factures_ponctuelles fp
    INNER JOIN
    fiche_client cl ON fp.id_client = cl.id
    INNER JOIN
    lignes_factures_ponctuelles lfp ON fp.id = lfp.id_facture_ponctuelle
WHERE
    fp.sepa is not true
GROUP BY fp.id
ORDER BY nom)
ORDER BY nom;
");
$requete->execute();
$results=$requete->fetchall(PDO::FETCH_ASSOC);

foreach($results as $key => $facture)
{	
	if(!strpos($results[$key]['facture'],'temp') && !empty($results[$key]['facture']))
	{
		$results[$key]['facture_envoi'] = 'ok';
		
		//r�cup�ration des �l�ments du client
		$details_client = ClientDao::searchByName($facture['nom']);
		if($details_client)
		{
			$results[$key]['num_client'] = $details_client[0]['code_client_hto'];
			
			//v�rifie si le d�tail existe
			if(glob("facture_temp/csv/".date("mY")."/".$results[$key]['num_client']."_".date("mY")."_regroup.csv"))
			{
				$results[$key]['detail'] = 'oui';
			}
		}
		
	}
}

//var_dump($results);

if(glob("facture_temp/csv/".date("mY")."/".date("mY")."_total.csv"))
{
	$detail_total = 'oui';
}
			
//pour le total des lignes mobiles
$requete = $bdd->query("SELECT SUM(total_facturation) AS total_facturation FROM admin_facture WHERE type = 'mobile' AND facture NOT LIKE '%temp%' ");
$data = $requete->fetch(); 
$total_mobile = $data['total_facturation'];

//pour le total des lignes fixes
$requete = $bdd->query("SELECT SUM(total_facturation) AS total_facturation FROM admin_facture WHERE type = 'fixe' AND facture NOT LIKE '%temp%' ");
$data = $requete->fetch(); 
$total_fixe = $data['total_facturation'];

//calcul HT et TTC
$total_mobile_ttc = number_format($total_mobile*1.2,2,',',' ');
$total_fixe_ttc = number_format($total_fixe*1.2,2,',',' ');
$total_mobile = number_format($total_mobile,2,',',' ');
$total_fixe = number_format($total_fixe,2,',',' ');

// Récupération du total des nouveaux clients
$nbNouveauxClients = 0;
$requete = $bdd->query("SELECT COUNT(id) AS total_clients FROM fiche_client where nouveau_client IS TRUE;");
$ret = $requete->fetch();
if (!empty($ret)) {
    $nbNouveauxClients = $ret['total_clients'];
}

// Rendu de la page
echo $twig->render('lancement-facture.html.twig', 
		array(	'factures' => $results,
				'detail_total' => $detail_total,
				'total_mobile' => $total_mobile,
				'total_mobile_ttc' => $total_mobile_ttc,
				'total_fixe' => $total_fixe,
				'total_fixe_ttc' => $total_fixe_ttc,
				'nb_nouveaux_clients' => ($nbNouveauxClients > 0),
				'message' => $message, 
				'error' => $error));

}
else {
	redirect("home-admin");
}